﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;

using Palsta.Core.manifest;
using Palsta.Core.data;
using Palsta.Core.view;

namespace Palsta.Core
{
    class InfoPathPageDumper
    {

        public Boolean DumpToPath(InfoPathPage page, String path)
        {
            Directory.CreateDirectory(path);
            if (File.Exists(path + "jquery-1.8.2.js"))
            {
                File.Delete(path + "jquery-1.8.2.js");
            }
            if (File.Exists(path + "xslt-lib.js"))
            {
                File.Delete(path + "xslt-lib.js");
            }
            File.Copy(AppDomain.CurrentDomain.BaseDirectory + "lib\\refs\\jquery-1.8.2.js", path + "jquery-1.8.2.js");
            File.Copy(AppDomain.CurrentDomain.BaseDirectory + "lib\\refs\\xslt-lib.js", path + "xslt-lib.js");


            String htmlFileName = path + "view.html";
            FileStream fs = new FileStream(htmlFileName, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);

            IDictionaryEnumerator i = page.HtmlMap.GetEnumerator();
            while (i.MoveNext())
            {
                String fileName = (String)i.Key;
                sw.WriteLine("<?-- " + fileName + " -->");
                sw.WriteLine((String)i.Value);
            }
            sw.Close();
            fs.Close();


            FileStream actionfs = new FileStream(path + "action.js", FileMode.Create);
            StreamWriter actionsw = new StreamWriter(actionfs);

            foreach (String js in page.Action.GetConditionFunJs())
            {
                actionsw.WriteLine(js);
            }
            foreach (String js in page.Action.GetEvalFunJs())
            {
                actionsw.Write(js);
            }
            foreach (String js in page.Action.GetAssignmentFunJs())
            {
                actionsw.Write(js);
            }
            foreach (String js in page.Action.GetValidationFunJs())
            {
                actionsw.Write(js);
            }
            foreach (String js in page.Action.GetCaculationFunJs())
            {
                actionsw.Write(js);
            }
            foreach (String js in page.Action.GetOnChangeFunJs())
            {
                actionsw.Write(js);
            }
            actionsw.Write(page.Action.InitFunctionJS);
            actionsw.Close();
            actionfs.Close();
            return true;
        }


                    
            //String dataPath = upzipPath + "sampledata.xml";
            //String schemaPath = upzipPath + "myschema.xsd";
            //InfoPathData ipd = new InfoPathData();
            //ipd.Parse(dataPath, schemaPath);
            //String viewPath = upzipPath + "view1.xsl";
            //InfoPathView ipv = new InfoPathView();
            //ipv.Translate(viewPath, ipd, outputDir);
            //File.Copy(AppDomain.CurrentDomain.BaseDirectory + "\\refs\\jquery-1.8.2.js", outputDir + "\\jquery-1.8.2.js");
            //File.Copy(AppDomain.CurrentDomain.BaseDirectory + "\\refs\\xslt-lib.js", outputDir + "\\xslt-lib.js");
    }
}
